package com.hnkywl.sts.action.htdygl;

import com.hnkywl.sts.entity.htgl.Htdy;
import com.hnkywl.sts.service.htdygl.DynaFormService;
import com.hnkywl.sts.service.htdygl.HtdyService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import com.hnkywl.sts.service.htdygl.HtmbdyService;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-4-17
 * Time:  上午 11:28
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/htdygl/htmb")
public class HtmbdyAction extends SimpleActionSupport {

    // 动态页面service
    private DynaFormService dynaFormService;

    // 合同定义service
    private HtdyService htdyService;

    // 合同定义id
    private String htdyId;

    // 合同模板信息以及值
    private String htContent;

    //合同定义service
    private HtmbdyService htmbdyservice;

    // 合同模板名称
    private String htmbmc;

    // 合同模板id
    private String htmbId;

    // 保存合同模板名称
    private String htmb_mc;

    // 保存合同定义
    private String htmb_htdy;

    // 保存合同备注
    private String htmb_bz;


    // 分页数
    private int page;

    // 分页组件 页面大小
    private int rows;

    // 查询所有的合同模板
    @Action("htmbList")
    public void htmbdyList() {
        try {
            this.renderJson(this.htmbdyservice.findAllHtmb(htmbmc, htdyId, page, rows));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    // 合同模板详细信息
    @Action("htmbdyDetail")
    public void htmbdyDetail() throws IOException {
        if (StringUtils.isEmpty(htmbId)) {
            Htdy htdyDetail = this.htdyService.findHtdyById(htdyId);
            this.renderHtml(dynaFormService.getFormHtml(htdyDetail, null));
        } else {
            String htdyid = this.htmbdyservice.getHtdyId(htmbId);
            if (StringUtils.isEmpty(htdyid)) {
                this.renderHtml("没有合同模板");
            } else {
                Htdy htdyDetail = this.htdyService.findHtdyById(htdyid);
                if (null != htdyDetail) {
                    this.renderHtml(dynaFormService.getFormHtml(htdyDetail, htmbId));
                } else {
                    this.renderHtml("没有合同模板");
                }
            }
        }
    }


    // 合同模板详细信息
    @Action("htmbdy_content")
    public String htmbdy_content() {
        if (StringUtils.isNotEmpty(htmbId)) {
            List htmbList = this.htmbdyservice.findHtmbById(htmbId);
            Object[] objects = (Object[]) htmbList.get(0);
            htmb_mc = objects[1].toString();
            htmb_htdy = objects[2].toString();
            if (null != objects[3] && StringUtils.isNotEmpty(objects[3].toString())) {
                htmb_bz = objects[3].toString();
            }
        }
        return SUCCESS;
    }

    // 保存合同模板基本信息
    @Action("addOrUpdateHtmb")
    public void addOrUpdateHtmb() {
        try {
            String id = this.htmbdyservice.addOrUpdateHtmb(htmbId, htmb_mc, htmb_htdy, htmb_bz);
            renderJson("{\"statusCode\":\"200\", \"message\":\"保存合同模板义成功!\",\"id\":\"" + id + "\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }


    // 删除合同模板
    @Action("deleteHtmb")
    public void deleteHtmb() {
        try {
            if (StringUtils.isNotEmpty(htmbId)) {
                this.htmbdyservice.deleteHtwb(htmbId);
                renderJson("{\"statusCode\":\"200\", \"message\":\"删除合同模板义成功!\"}");

            } else {
                renderJson("{\"statusCode\":\"300\", \"message\":\"删除合同模板的id为空!\"}");
            }

        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    // 保存合同模板
    @Action("saveHtmb")
    public void saveHtmb() {
        if (StringUtils.isNotEmpty(htmbId) && StringUtils.isNotEmpty(htContent) && StringUtils.isNotEmpty(htdyId)) {
            Map<String, String> formDataMap = new HashMap<String, String>();
            String[] formData = getHtContent().split(",o");
            for (int i = 0; i < formData.length; i++) {
                String[] key_value = formData[i].split(":o", 2);
                formDataMap.put(key_value[0], key_value[1]);
            }
            try {
                this.dynaFormService.saveHtmbData(formDataMap, htmbId, htdyId);
                renderJson("{\"statusCode\":\"200\", \"message\":\"" + "保存合同模板内容成功！" + "\"}");
            } catch (Exception e) {
                renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
            }
        } else {
            renderJson("{\"statusCode\":\"300\", \"message\":\"保存数据错误！\"}");
        }
    }

    // 查询合同模板
    @Action("comboboxHtmbList")
    public void comboboxHtmbList() {
        try {
            if (null == this.htmbdyservice.findAllHtmbByHtdyID(htdyId)) {
                this.renderJson("[]");
            } else {
                this.renderJson(this.htmbdyservice.findAllHtmbByHtdyID(htdyId));
            }
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }

    }

    public String getHtdyId() {
        return htdyId;
    }

    public void setHtdyId(String htdyId) {
        this.htdyId = htdyId;
    }

    public String getHtContent() {
        return htContent;
    }

    public void setHtContent(String htContent) {
        this.htContent = htContent;
    }

    public String getHtmbmc() {
        return htmbmc;
    }

    public void setHtmbmc(String htmbmc) {
        this.htmbmc = htmbmc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getHtmbId() {
        return htmbId;
    }

    public void setHtmbId(String htmbId) {
        this.htmbId = htmbId;
    }

    public String getHtmb_mc() {
        return htmb_mc;
    }

    public void setHtmb_mc(String htmb_mc) {
        this.htmb_mc = htmb_mc;
    }

    public String getHtmb_htdy() {
        return htmb_htdy;
    }

    public void setHtmb_htdy(String htmb_htdy) {
        this.htmb_htdy = htmb_htdy;
    }

    public String getHtmb_bz() {
        return htmb_bz;
    }

    public void setHtmb_bz(String htmb_bz) {
        this.htmb_bz = htmb_bz;
    }

    @Autowired
    public void setHtdyService(HtdyService htdyService) {
        this.htdyService = htdyService;
    }

    @Autowired
    public void setHtmbdyservice(HtmbdyService htmbdyservice) {
        this.htmbdyservice = htmbdyservice;
    }

    @Autowired
    public void setDynaFormService(DynaFormService dynaFormService) {
        this.dynaFormService = dynaFormService;
    }
}
